<?php
$loader->requireOnce('includes/Icd10Selector/Icd10DataSource.class.php');
class Icd10Selector {
	protected $_lang = 'vi';
	protected $_ds = '';
	protected $_db;
	
	const EYE_CHAPTER = 7;
	
	function Icd10Selector($lang = 'vi') {
		if ($lang != 'vi') {
			$this->_lang = 'en';
		}
		$this->_ds = new Icd10DataSource($this->_lang);		
		$this->_db = $GLOBALS['frame']['adodb']['db'];  
	}
	
	public function getAllGroups($htmlId, $act = '') {
		return $this->renderGroupList($this->_ds->getAllGroups(), $htmlId, $act);
	}
	
    public function getGroupsByChapterId($chapterId, $htmlId, $act = '') {
        return $this->renderGroupList($this->_ds->getGroupsByChapterId($chapterId), $htmlId, $act);
    }
	
	public function getBlocks($htmlId, $groupId = 0, $act = '') {
		return $this->renderItemList($this->_ds->getBlocks($groupId), $htmlId, 'block', $act);
	}
	
	public function getItems($htmlId, $code = 'H00', $act = '') {
		return $this->renderItemList($this->_ds->getItems($code), $htmlId, 'item', $act);
	}
	
	private function renderGroupList($ds, $htmlId, $act = '') {
		$html = "<ul class='icd10' id='$htmlId'>";
		if(!empty($ds)) {
			if ($act == '') {
				foreach ($ds as $row) {
					$html.="<li id='group_{$row['id']}'><span>{$row['code']} - <span><span class='name'>{$row['name']}</span></li>";	
				}
			} else {
				foreach ($ds as $row) {
					$html.="<li id='group_{$row['id']}' onclick='$act({$row['id']})'><span>{$row['code']} - <span><span class='name'>{$row['name']}</span></li>";	
				}
			}
		}
		$html.="<li id='group_normal' onclick='getNormal()'>".L10nHelper::_("Normal")."</li>";
		$html .= "</ul>";
		return $html;
	}
	
	private function renderItemList($ds, $htmlId, $itemType, $act = '') {
		$html = "<ul class='icd10' id='$htmlId'>";
		if(!empty($ds)) {
			if ($act == '') {
				foreach ($ds as $row) {
					$html.="<li id='{$itemType}_{$row['id']}'><span>{$row['code']} - <span><span class='name'>{$row['name']}</span></li>";	
				}
			} else {
				foreach ($ds as $row) {
					$html.="<li id='{$itemType}_{$row['id']}' onclick=\"$act('{$row['code']}', {$row['id']})\"><span>{$row['code']} - </span><span class='name'>{$row['name']}</span></li>";	
				}
			}
		}
		$html .= "</ul>";
		return $html;
	}
	
    public function initIcd10ForController(&$controller, $initChapter, $groupId = 0) {
        if ($controller instanceof Controller) {
            $icd10Chapter = Celini::newOrdo("Icd10Chapter")->listAllChapters();
            $icd10BigGroup = $this->getGroupsByChapterId($initChapter, 'icd10Group', 'loadBlocks');            
            $icd10SmallGroup = '';    
            if ($groupId > 0) {
                $icd10SmallGroup = $this->getBlocks('icd10Block', $groupId, 'loadItems'); 
            }        
            $controller->assign('icd10Chapter', $icd10Chapter);                        
            $controller->assign('initIcd10Chapter', $initChapter);
            
            $controller->assign('icd10BigGroup', $icd10BigGroup);  
            $controller->assign('initIcd10BigGroup', $groupId);                      
            $controller->assign('icd10SmallGroup', $icd10SmallGroup);                                    
        }
    }
    
	public function validateIcd10Code($code){				
		$sql = "SELECT code FROM icd10 WHERE code = '$code' LIMIT 1";		
		$res = $this->_db->Execute($sql);
		if (!$res->EOF) {
		    return true;
		}		
		return false;
	}
		
}
?>